import urllib2, os, zipfile
from lxml import etree
import csv
from xml.etree.ElementTree import iterparse
import sys

def xmlSplitter(data,separator=lambda x: x.startswith('<?xml')):
  buff = []
  for line in data:
    if separator(line):
      if buff:
        yield ''.join(buff)
        buff[:] = []
    buff.append(line)
  yield ''.join(buff)

def first(seq,default=None):
  """Return the first item from sequence, seq or the default(None) value"""
  for item in seq:
    return item
  return default


datasrc = "ipgb20110104_wk01.zip"
filename = datasrc.split('/')[-1]

csvWriter = csv.writer(open("csvfile.csv", 'wb'), quoting=csv.QUOTE_ALL)

zf = zipfile.ZipFile(filename)
xml_file = first([ x for x in zf.namelist() if x.endswith('.xml')])
assert xml_file is not None

count = 0
print "DocID,Title,Last Name,Street,City,State,Post Code,Country"

for item in xmlSplitter(zf.open(xml_file)):
  count += 1
  if count > 100: break
  doc = etree.XML(item)
  docID = first(doc.xpath('//publication-reference/document-id/doc-number/text()'))
  title = first(doc.xpath('//invention-title/text()'))
  applicantSeq = doc.xpath('//parties/applicants/applicant/@sequence')
  lastName = doc.xpath('//parties/applicants/applicant/addressbook/last-name/text()')
  firstName = doc.xpath('//parties/applicants/applicant/addressbook/first-name/text()')
  street = doc.xpath('//parties/applicants/applicant/addressbook/address/street/text()')
  city = doc.xpath('//parties/applicants/applicant/addressbook/address/city/text()')
  state = doc.xpath('//parties/applicants/applicant/addressbook/address/state/text()')
  postcode = doc.xpath('//parties/applicants/applicant/addressbook/address/postcode/text()')
  country = doc.xpath('//parties/applicants/applicant/addressbook/address/country/text()')
  print u"DocID:    {0}\nTitle:    {1}\nApplicant Sequence:     {2}\nLast Name: {3}\nFirst Name: {4}\nStreet: {5}\ncity: {6}\nstate: {7}\npostcode: {8}\ncountry: {9}\n".format(docID,title,applicantSeq,lastName,firstName,street,city,state,postcode,country)
  csvWriter.writerow((docID,title,applicantSeq,lastName,firstName,street,city,state,postcode,country))
  #stringContent= (docID+","+title+","+lastName+","+firstName+","+str(street)+","+city+","+str(postcode)+","+country+"\n") + stringContent
  #print applicantSeq
  #print docID+","+title+","+str(applicantSeq)+","+str(lastName)+","+firstName+","+str(street)+","+city+","+str(postcode)+","+country
  #print stringContent

  
